package com.polaris.live.ui.party.activity.video

import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import androidx.core.os.bundleOf
import com.polaris.live.R
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.ActivityPartyAnchorBinding
import com.polaris.live.im.sfs.bean.live.LiveRoomInfo
import com.polaris.live.manager.LiveStateManager
import com.polaris.live.resp.back_resp.ScreenshotStrategyVo
import com.polaris.live.ui.live.bean.LiveInitInfo
import com.polaris.live.ui.party.PartyUserStateUtil
import com.polaris.live.ui.party.activity.base.BasePartyAnchorActivity
import com.polaris.live.ui.party.fragment.video.PartyVideoAnchorFragment
import com.polaris.live.ui.party.listener.OnRoomFinishCallback
import com.polaris.live.utils.fragment.attachFragment
import com.polaris.live.viewmodel.party.PartyAnchorModel

/**
 * PartyVideoAnchorActivity视频派对主播
 * @author Created by 天晴 on 2024/6/21 9:21
 * @since 1.0.0
 **/
class PartyVideoAnchorActivity :
    BasePartyAnchorActivity<PartyAnchorModel, ActivityPartyAnchorBinding>() {

    private var mScreenshotStrategy: ScreenshotStrategyVo? = null

    /**
     * 直播间房间信息
     */
    private lateinit var mLiveInfo: LiveRoomInfo
    private val mFragment by lazy {
        supportFragmentManager.attachFragment(R.id.fl_fragment) {
            PartyVideoAnchorFragment().apply {
                arguments = bundleOf(
                    LiveConst.Params.LIVE_INFO to mLiveInfo,
                    LiveConst.Params.SCREENSHOT_STRATEGY to mScreenshotStrategy
                )
                setOnRoomFinishCallback(object : OnRoomFinishCallback {
                    override fun onRoomFinishCallback() {
                        finish()
                    }
                })
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        val initVariable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getParcelableExtra(LiveConst.Params.LIVE_INIT_INFO, LiveInitInfo::class.java)
        } else {
            intent.getParcelableExtra(LiveConst.Params.LIVE_INIT_INFO)
        }
        if (initVariable == null) {
            finish()
            return
        }
        LiveStateManager.changeStateForce(LiveConst.State.PARTY)
        mLiveInfo = initVariable.info
        mScreenshotStrategy = initVariable.screenshotStrategy
        mFragment
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        mViewModel.launchLiveHeartbeat(mLiveInfo.liveId) //心跳
        showMusicWindowView()
    }

    override fun onDestroy() {
        super.onDestroy()
        PartyUserStateUtil.inTheMaiSeat = false
        PartyUserStateUtil.siteMicStatus = null
        LiveStateManager.changeState(LiveConst.State.NONE)
    }

    override fun onNetwork(isNetwork: Boolean, isConnected: Boolean) {
        mFragment.onNetwork(isNetwork, isConnected)
    }

    override fun onBackPressed() {
        if (!mFragment.onBackPressed()) {
            super.onBackPressed()
        }
    }
}